/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.utlity;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

/**
 * Created by JiangQi on 8/2/18.
 */
public class AnimatorUtility {
    public static void resetViewStatus(Component component) {
        component.setTranslationX(0);
        component.setTranslationY(0);
        component.setScaleX(1.0f);
        component.setScaleY(1.0f);
        component.setRotation(0.0f);
        component.setAlpha(1.0f);
    }

    
    public static AnimatorInfo captureViewStatus(Component component) {
        return new AnimatorInfo(component.getTranslationX(),
                component.getTranslationY(),
                component.getScaleX(),
                component.getScaleY(),
                component.getRotation(),
                component.getAlpha());
    }

    public static void resetViewStatus(Component component, AnimatorInfo animatorInfo) {
        component.setTranslationX(animatorInfo.translationX);
        component.setTranslationY(animatorInfo.translationY);
        component.setScaleX(animatorInfo.scaleX);
        component.setScaleY(animatorInfo.scaleY);
        component.setRotation(animatorInfo.rotation);
        component.setAlpha(animatorInfo.alpha);
    }

    public static void bringToFrontIfNeeded(Component component) {
        ComponentContainer viewGroup = (ComponentContainer) component.getComponentParent();
        int childCount = viewGroup.getChildCount();
        int childIndex = viewGroup.getChildIndex(component);
        if (childIndex >= 0 && childIndex != childCount - 1) {
            viewGroup.moveChildToFront(component);
        }
    }

    public static class AnimatorInfo {
        public final float translationX;
        public final float translationY;
        public final float scaleX;
        public final float scaleY;
        public final float rotation;
        public final float alpha;

        public AnimatorInfo(float translationX, float translationY,
                            float scaleX, float scaleY,
                            float rotation,
                            float alpha) {
            this.translationX = translationX;
            this.translationY = translationY;
            this.scaleX = scaleX;
            this.scaleY = scaleY;
            this.rotation = rotation;
            this.alpha = alpha;
        }
    }
}
